﻿using NetDimension.NanUI;
using NetDimension.NanUI.Browser;
using NetDimension.NanUI.HostWindow;
using NetDimension.NanUI.JavaScript;
using PLM.Configuration.Center.NanUIApp.Formiums;
using PLM.ConfigurationCenter.Core.Expansion;
using PLM.ConfigurationCenter.Model;
using PLM.ConfigurationCenter.SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PLM.Configuration.Center.NanUIApp.Windows {
    public class ServerWindow : Formium {
        /// <summary>
        /// 
        /// </summary>
        private ServerModel model = new ServerModel();
        /// <summary>
        /// 
        /// </summary>
        private string url = "";
        // 设置窗体样式类型
        public override HostWindowType WindowType => HostWindowType.System;

        public override string StartUrl => $"http://res.app.local/pages/{url.ToLower()}.html";

        public ServerWindow(string path) {
            Init();
            // 在此处设置窗口样式
            Size = new Size(800, 600);
            this.StartPosition = FormStartPosition.CenterParent;
            url = path;
        }
        /// <summary>
        /// 
        /// </summary>
        private async void Init() {
            model = await DbManager.DbContext.Queryable<ServerModel>().FirstAsync();
        }
        protected override void OnReady() {
            var fevent = new FormiumEvent(this);
            //ShowDevTools();
            var obj = new JavaScriptObject();
            fevent.RegisterMethod(obj, "GetSeverModel", GetSeverModel);
            var list = DbManager.DbContext.Queryable<ServerModel>().ToList();
            //注册值
            obj.Add("data", list.ToJson());
            RegisterJavaScriptObject("serverwindow", obj);

            LoadEnd += PageLoadEnd;
            //注册公共事件
            fevent.RegisterCommonEvent();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PageLoadEnd(object? sender, LoadEndEventArgs e) {
            if (e.Frame.IsMain) {
                SplashScreen.Hide();
            }
        }
        #region
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ServerModel GetSeverModel(string id) {
            var model = DbManager.DbContext.Queryable<ServerModel>().Where(p => p.Id == id).First();
            if (model.SiteName.IsEmpty() || model.SitePort == 0 || model.SiteLabel.IsEmpty()) {
                FillDefaultServer(model);
            }
            return model;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static void FillDefaultServer(ServerModel model) {
            switch (model.Id) {
                case "REDIS":
                    model.SiteName = "REDIS";
                    model.SitePort = 6379;
                    model.SiteLabel = "Redis服务";
                    break;
                case "WEB":
                    model.SiteName = "PLM_WEB_VUE";
                    model.SitePort = 3039;
                    model.SiteLabel = "前端服务";
                    break;
                case "API":
                    model.SiteName = "PLM_WEB";
                    model.SitePort = 3031;
                    model.SiteLabel = "后端服务";
                    break;
                case "ADAPTER":
                    model.SiteName = "ADAPTER";
                    model.SitePort = 3132;
                    model.SiteLabel = "ERP对接服务";
                    break;
                case "COMMONITEM":
                    model.SiteName = "COMMONITEM";
                    model.SitePort = 3301;
                    model.SiteLabel = "缓存服务";
                    break;
                case "FILESTORE":
                    model.SiteName = "FILESTORE";
                    model.SitePort = 8111;
                    model.SiteLabel = "文件服务";
                    break;
                case "INSTALLATION":
                    model.SiteName = "INSTALLATION";
                    model.SitePort = 3130;
                    model.SiteLabel = "同步中心";
                    break;
                case "JOBCENTER":
                    model.SiteName = "JOBCENTER";
                    model.SitePort = 3031;
                    model.SiteLabel = "定时任务中心";
                    break;
                default:
                    break;
            }
        }

        public void BtnSaveClick() {

        }
        #endregion
    }
}
